package com.glorysoft.tonglingxjpda.ui.function.viewmodel

import androidx.databinding.ObservableArrayList
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseViewModel
import com.glorysoft.tonglingxjpda.entity.BatchListEntity
import com.glorysoft.tonglingxjpda.entity.BoxInfoEntity
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.model.TaskRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: PackageViewModel
 * Author: XJH
 * Date: 2022/5/5 17:44
 * Description:
 * History:
 */
class PackageViewModel : BaseViewModel() {


    val PacListTitle: List<String> =
        arrayListOf("BatchID", "客户批次", "刻印号", "清洗次数", "部件编码", "部件名称", "数  量", "备  注")
    val FinPacListTitle: List<String> = arrayListOf("箱  号", "客户批次号", "装置名")
    val BoxListTitle: List<String> = arrayListOf("BatchID", "部件名称", "数量")
    private val pacRepository = TaskRepository()

    var boxData = ObservableArrayList<BoxInfoEntity>()
    var boxBinding =
        ItemBinding.of<BoxInfoEntity>(BR.boxItem, R.layout.item_package_box)

    var PacData = ObservableArrayList<BoxInfoEntity>()
    var PacBinding =
        ItemBinding.of<BoxInfoEntity>(BR.pacItem, R.layout.item_package_pac)

    var subData = ObservableArrayList<BoxInfoEntity>()
    var subBinding =
        ItemBinding.of<BoxInfoEntity>(BR.subItem, R.layout.item_package_sub)

    fun initBoxData(returnNo: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "GETRETURNNO")
        map.addProperty("RETURNNO", returnNo)
        launchWithError({
            val packageInfo = pacRepository.getPackageInfo(map)
            boxData.clear()
            boxData.addAll(packageInfo.RETURNLIST)
            subData.clear()
            subData.addAll(packageInfo.BOXLIST)
        }, {
            boxData.clear()
            subData.clear()
            PacData.clear()
        })

    }

    fun initPacData(returnNo: String, batchId: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "GETBOXBYBATCH")
        map.addProperty("RETURNNO", returnNo)
        map.addProperty("BATCHID", batchId)
        map.add("BATCHLIST", JsonParser().parse(Gson().toJson(PacData)).asJsonArray)
        launchWithError({
            val packageInfo = pacRepository.getPackageInfo(map)
            PacData.clear()
            PacData.addAll(packageInfo.BATCHLIST)
            boxData.clear()
            boxData.addAll(packageInfo.RETURNLIST)
        }, {
            PacData.clear()
            boxData.clear()
            subData.clear()
        })
    }

    fun submit(returnNo: String, batchId: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "BOXSHIP")
        map.addProperty("RETURNNO", returnNo)
        map.addProperty("BATCHID", batchId)
        map.add("BATCHLIST", JsonParser().parse(Gson().toJson(PacData)).asJsonArray)
        launchWithError({
            val packageInfo = pacRepository.getPackageInfo(map)
            subData.clear()
            PacData.clear()
            boxData.clear()
            subData.addAll(packageInfo.BOXLIST)
            PacData.addAll(packageInfo.BATCHLIST)
            boxData.addAll(packageInfo.RETURNLIST)
            defUI.toastEvent.value = ToastMessageBean("包装成功", true)
        }, {
            subData.clear()
            PacData.clear()
            boxData.clear()
        })
    }

}